package com.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.core.baseDao.extend.Pager;
import com.manager.ClassifyManager;
import com.model.Classify;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@ParentPackage("json-default")
@Results({ @Result(name = "jsonResult", type = "json") })
@Namespace("/user")
public class ClassifyAction extends ActionSupport{
	
	@Autowired
	private ClassifyManager cManager;
	
	private Classify classify;
	
	public ClassifyManager getcManager() {
		return cManager;
	}

	public void setcManager(ClassifyManager cManager) {
		this.cManager = cManager;
	}

	public Classify getClassify() {
		return classify;
	}

	public void setClassify(Classify classify) {
		this.classify = classify;
	}

	public String add(){
		
		return "add";
	}
	
	public String addTo(){
		String ret="";
		Map session = ActionContext.getContext().getSession();
		String token = (String)session.get("token");
		classify.setToken(token);
		if(classify.getId() != null){
			ret = "分类信息更新成功！";
		}else{
			ret = "分类添加成功！";
		}
		cManager.saveOrUpdate(classify);
		ServletActionContext.getRequest().setAttribute("ret", ret);
		return "add";
	}
	
	public String index(){

		ServletRequest request = ServletActionContext.getRequest();
		Pager page = new Pager();
		String p = request.getParameter("p");
		if(!StringUtils.isEmpty(p)){
			page.setCurrPage(Integer.parseInt(p));
		}
		Map session = ActionContext.getContext().getSession();
		String token = session.get("token")+"";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("token", token);
		page = cManager.findByPage("from Classify ", params, page);
		request.setAttribute("page", page);
		
		return "index";
	}
	
	public String edit(){
		ServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		Map session = ActionContext.getContext().getSession();
		String token = (String)session.get("token");
		Classify vo = (Classify)cManager.findUnique("from Classify k where k.id=? and k.token=?", new Object[]{Integer.parseInt(id),token});
		request.setAttribute("vo", vo);
		return "edit";
	}
	
	public String delete(){
		Map session = ActionContext.getContext().getSession();
		String token = (String)session.get("token");
		String id = ServletActionContext.getRequest().getParameter("id");
		Classify vo = (Classify)cManager.findUnique("from Classify k where k.id=? and token=?", new Object[]{Integer.parseInt(id),token});
		cManager.delete(vo);
		return "index";
	}
}
